import defaultAxios from 'axios';
import { message } from 'antd';
import Nprogess from 'nprogress';
import 'nprogress/nprogress.css';
const axios = defaultAxios.create({
  baseURL: 'http://api.shbwyz.com/api/',
});
// 白名单
const whiteList = [
  {
    method: 'get',
    url: '/getserviceLog',
  },
  {
    method: 'get',
    url: '/getChat',
  },
];

defaultAxios.interceptors.request.use((config) => {
  Nprogess.start();
  return config;
});
defaultAxios.interceptors.response.use(
  (data: any) => {
    //看是否存在白名单
    const flag = whiteList.some(
      (item) =>
        item.method === data.config.method &&
        item.url.includes(data.config.url),
    );
    Nprogess.done();
    if (flag) {
      message.success('请求成功');
    }
    return data;
  },
  (error) => {},
);

axios.interceptors.request.use((config) => {
  config.headers.Authorization = sessionStorage.getItem('token')
    ? sessionStorage.getItem('token')
    : '';
  return config;
});

axios.interceptors.response.use((config) => {
  return config;
});

// 因为是不同的2套接口 请求自行本地模拟接口用defaultAxios
export { defaultAxios, axios };
